package com.github.cooingandwooing.common.mybatisplus.config;

import java.util.Date;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.handler.TenantLineHandler;
import com.baomidou.mybatisplus.extension.plugins.inner.OptimisticLockerInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.TenantLineInnerInterceptor;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.expression.LongValue;
import org.apache.ibatis.reflection.MetaObject;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author cooingandwooing
 */
@Configuration
public class MybatisPlusConfig implements MetaObjectHandler {
    /**
     * 新多租户插件配置,一缓和二缓遵循mybatis的规则,需要设置 MybatisConfiguration#useDeprecatedExecutor = false 避免缓存万一出现问题
     */
    @Bean
    public MybatisPlusInterceptor mybatisPlusInterceptor() {
        MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
        interceptor.addInnerInterceptor(new OptimisticLockerInnerInterceptor());
        interceptor.addInnerInterceptor(new TenantLineInnerInterceptor(new TenantLineHandler() {
            @Override
            public Expression getTenantId() {
                return new LongValue(1);
            }

            // 这是 default 方法,默认返回 false 表示所有表都需要拼多租户条件
            @Override
            public boolean ignoreTable(String tableName) {
                return !"user".equalsIgnoreCase(tableName);
            }
        }));
        // 如果用了分页插件注意先 add TenantLineInnerInterceptor 再 add PaginationInnerInterceptor
        interceptor.addInnerInterceptor(new PaginationInnerInterceptor(DbType.MYSQL));
        return interceptor;
    }

/*    @Bean
    @ConditionalOnProperty(prefix = "mybatis-plus", name = "performance", havingValue = "true")
    public PerformanceInterceptor performanceInterceptor() {
        return new PerformanceInterceptor();
    }*/

    @Override
    public void insertFill(MetaObject metaObject) {
        //默认未删除
        setFieldValByName("isDeleted", 0, metaObject);
        //判断添加/更新的时候是否给他赋值
        Object createTime1 = getFieldValByName("createdTime", metaObject);
        if (createTime1 == null) {
            //this.strictInsertFill(metaObject, "createdTime", Long.class,System.currentTimeMillis());
            //创建时间默认当前时间
            setFieldValByName("createdTime", new Date(), metaObject);
        }

        setFieldValByName("createdBy", "admin", metaObject);
        //setFieldValByName("createdBy", UserInfoContext.getUserInfo().getId(), metaObject);
        setFieldValByName("updatedTime", new Date(), metaObject);
        //setFieldValByName("updatedBy", UserInfoContext.getUserInfo().getId(), metaObject);
        setFieldValByName("updatedBy", "admin", metaObject);
    }

    @Override
    public void updateFill(MetaObject metaObject) {

        //创建时间默认当前时间
        setFieldValByName("updatedTime", new Date(), metaObject);
        //创建人
        //setFieldValByName("updatedBy", UserInfoContext.getUserInfo() != null ? UserInfoContext.getUserInfo().getId() : "", metaObject);
        setFieldValByName("updatedBy", "admin", metaObject);
    }
}
